//
//  UIViewController+ImageViewAniamtion.m
//  AffectionBee
//
//  Created by 王炯 on 16/8/23.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "UIViewController+ImageViewAniamtion.h"
#import <objc/runtime.h>

@implementation UIViewController (ImageViewAniamtion)

static NSString *strKey = @"Layers";


#warning 用运行时添加属性，并添加属性的get方法和set方法

//set方法
-(void)setAnimationLayers:(NSMutableArray<CALayer *> *)animationLayers
{
    objc_setAssociatedObject(self, &strKey, animationLayers,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//get方法
-(NSMutableArray<CALayer *> *)animationLayers
{
    return objc_getAssociatedObject(self, &strKey);
}



//cell上的商品添加到购物车的动画
-(void)addProaddProductsAnimation:(UIImageView *)cellImageView
{
    
    if (self.animationLayers == nil)
    {
        self.animationLayers = [[NSMutableArray alloc]init];
    }
    
    //根据传入的imageView来转化成frame
    CGRect frame = [cellImageView convertRect:cellImageView.bounds toView:self.view];
    
    //创建转换图层
    CALayer *transitionLayer = [[CALayer alloc] init];
    transitionLayer.frame = frame;
    transitionLayer.contents = cellImageView.layer.contents;
    
    //将图层添加到当前view的图层上
    [self.view.layer addSublayer:transitionLayer];
    //添加到图层数组中
    [self.animationLayers addObject:transitionLayer];
    
    //取出位置
    CGPoint p1 = transitionLayer.position;
    CGPoint p3 = CGPointMake(self.view.frame.size.width - self.view.frame.size.width / 4 - self.view.frame.size.width / 8 - 6, self.view.layer.bounds.size.height - 40);
    
    //(1)添加关键动画(位置改变)
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //画一个位置改变的动画路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, p1.x, p1.y);
    CGPathAddCurveToPoint(path, nil, p1.x, p1.y - 30, p3.x, p1.y - 30, p3.x, p3.y);
    positionAnimation.path = path;
    
    //(2)添加基本动画(透明度改变)
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @1;
    opacityAnimation.toValue = @0.9;
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.removedOnCompletion = YES;
    
    //(3)添加基本动画(大小转变)
    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(CATransform3DIdentity, 0.2, 0.2, 1)];
    
    //(4)创建组动画(将以上动画包装到一起)
    CAAnimationGroup *groupAnimation = [[CAAnimationGroup alloc] init];
    
    groupAnimation.animations = @[positionAnimation,transformAnimation,opacityAnimation];
    
    //持续时间
    groupAnimation.duration = 0.8;
    //设置代理
    groupAnimation.delegate = self;
    
    //将动画添加到图层上
    [transitionLayer addAnimation:groupAnimation forKey:@"cartParabola"];
    
}

//实现代理方法，当动画消失的时候调用
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //当图层动画消失的时候，将图层的动画移除
    if (self.animationLayers.count > 0)
    {
        CALayer *transitionLayer = self.animationLayers[0];
        //隐藏动画图层
        transitionLayer.hidden = YES;
        //移除
        [transitionLayer removeFromSuperlayer];
        
        [self.animationLayers removeObjectAtIndex:0];
        
        //动画移除
        [self.view.layer removeAnimationForKey:@"cartParabola"];
    }
}





@end
